<?php session_start(); ?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Property</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="css/stylesheet.css" rel="stylesheet" type="text/css"/>
<link href="css/jq-mobile-green-button-theme.css" rel="stylesheet" type="text/css"/>
<link href="css/jq-mobile-red-button-theme.css" rel="stylesheet" type="text/css"/>
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.0rc2/jquery.mobile-1.0rc2.min.css" />
<script type="text/javascript" src="http://code.jquery.com/jquery-1.6.4.min.js"></script>
<script type="text/javascript" src="http://code.jquery.com/mobile/1.0rc2/jquery.mobile-1.0rc2.min.js"></script>
<!-- This reference to phonegap.js will allow for code hints as long as the current site has been configured as a mobile application. 
	 To configure the site as a mobile application, go to Site -> Mobile Applications -> Configure Application Framework... -->
<script src="js/phonegap.js" type="text/javascript"></script>
<script src="js/property.js" type="text/javascript"></script>
<script>

</script>
</head>
<body>
<div data-role="page" id="page"  data-theme="c">
<div data-role="header" rel="external" data-theme="a">
<a href="index.html" data-icon="back" rel="external" data-icon="back">Back</a>
 <a href="logout.php" data-role="button" data-icon="delete" rel="external">Logout</a>
		<h1>Match n' Rent</h1>
	</div>

        

    <div data-role="content" id = "searchResult">
	<form id ="propertyForm" action="property.php" method="post" data-ajax="false">
        <br />
            <select name="state" id="state" data-theme="c" style="width:100%">
                <!--<option value="NSW">New South Wales</option>-->
                <option value="QLD">Queensland</option>
                
                <input type="search" name="suburb" id="suburb" value="" data-theme="c" width="95%"/>
            </select>
            
        </form><br/>
    <ul data-role='listview' data-theme='a'>
<?php
if(isset($_POST["state"]) || isset($_POST["suburb"])){



$username = $_SESSION['username'];

$state = $_POST["state"];
$suburb = $_POST["suburb"];

include ("sqlconnection.php");


$db = new Database();
$db->connect();

$query = "select bid from behaviour";
$result = mysql_query($query);
$behaviours = array();
while($rows = mysql_fetch_array($result)){
 $behaviours[] = $rows["bid"];
}

$hashMap = array();
$userbeharray = array();
$tempMap = array();
$userQuery = "select * from users_behaviour where username = '$username'";
$userResult = mysql_query($userQuery);

while($userRow = mysql_fetch_array($userResult)){

	array_push($userbeharray, $userRow['bid']);
}

$query = "select * from property where state = '$state' and suburb = '$suburb'";
$result = mysql_query($query);

	while($row = mysql_fetch_array($result)){
	
		$pro_id = $row['pro_id'];
		
		$residentQuery = "select * from resident where pro_id = '$pro_id' and moveout_date IS NULL and username != '$username'";
		
		$residentResult = mysql_query($residentQuery);
		
		//for each resident find the username and get all their behaviour from the behnaviour table
		$meanAvg = 0;
	
		while($rRow = mysql_fetch_array($residentResult)){
   
   $residentUsername = $rRow['username'];
   $behQuery = "select * from users_behaviour where username = '$residentUsername'";
      
   $behResult = mysql_query($behQuery);
   $behArray = array();
   while($behRow = mysql_fetch_array($behResult)){
    
    $behArray[] = $behRow['bid'];
   }
   
   $similarBeh = array_intersect($behArray, $userbeharray);
   $diffBeh = array_merge(array_diff($behArray, $userbeharray), array_diff($userbeharray,$behArray));

   
   $mergedDiffAndSim = array_merge($similarBeh, $diffBeh);
   $notChosenBehaviour = array_diff($behaviours, $mergedDiffAndSim);
   
   $sim = array_merge($similarBeh, $notChosenBehaviour);
   
   $totalAttribute = count($sim) + count($diffBeh);
   
   $percentage = count($sim);
   $meanAvg += ($percentage/$totalAttribute)*100;
  
 
  }
		
		$i = mysql_num_rows($residentResult);
		if($i != 0){
			$houseAvg = $meanAvg/$i;
			$tempMap[$pro_id] = $houseAvg;
		}
	}
	arsort($tempMap);	
	foreach($tempMap as $key => $val){
		$query = "select * from property where pro_id = $key";
		$result = mysql_query($query);
		while($rows = mysql_fetch_array($result)){
			$add = $rows['address'];
			$pro_id = $rows['pro_id'];
			$toiletCount = $rows['toilet'];
			$bedsCount = $rows['bedroom'];
			$garageCount = $rows['garage'];
			
			echo "<li data-theme='c'>";
			
			echo "<a href='property1.php?pid=$pro_id&percent=$val' class='ui-link-inherit'>";
			$query2 = "select image_url from property_images where p_id = $pro_id";
			$result2 = mysql_query($query2);
			$rows2 = mysql_fetch_array($result2);
			$image_url= $rows2["image_url"];
								
			echo "<img src= '$image_url' style='padding:15px 0px 0px 5px;' />";
			echo "<h3> $add </h3>";
			echo "<p>";
			echo "<img src='./images/beds.png' /><span class='iconNum'>$bedsCount</span>";
			echo "<img src='./images/baths.png' /><span class='iconNum'>$toiletCount</span>";
			echo "<img src='./images/garage.png' /><span class='iconNum'>$garageCount</span>";
			echo "<br /><br /><span class='iconNum2' style='white-space:normal;'>No. of tenants currently living here:&nbsp;</span>";
			$pro_id = $rows['pro_id'];
			
			
			$query3 = "select u.profile_pic  from resident r , usertable u where r.username = u.username and r.pro_id = $pro_id and moveout_date IS NULL" ;
			$result3 = mysql_query($query3);
			
			$count = mysql_num_rows($result3);
			
			if($count == 0)
			{
				echo "<span class='iconNum2'>none</span>";
			}
			else
			{
				while($rows = mysql_fetch_array($result3)){
					$pic_url = $rows['profile_pic'];
					echo "<img src='$pic_url' height='20px' width='15px' style='padding-right:5px;' />";
				}
				
			}
			
			echo "<br /><br /><div><span class='iconNum2'>Compatibility: <br />".$val."% </span><div name='cbar' class='cbar' style='width:".$val."px;'></div></div>" ;
			
			echo "</p>";
			echo "</a>";
			echo "</li>";
			}
	}
	$db->disconnect();
}
			
?>
</ul>
    </div>
</div>
</div>
</body>
</html>